<template>
	<view v-if="is_login">
		<view class="mainpadding_top margin_top6">
			<view class="mainpadding textcenter xiaohei sanshier">可用余额￥{{userInfo.money}}</view>
			<view class="mainpadding_top ffffff radio margin_top6">
				<view class="xiaohei ershiba">支付金额</view>
				<view class="margin_top flexleft">
					<view class="xiaohei fonweight" style="font-size: 50rpx;">￥</view>
					<input type="number" placeholder="输入金额" v-model="pay_price" style="font-size: 50rpx;height: 60rpx;line-height: 60rpx;" class="xiaohei fonweight"/>
				</view>
			</view>
			<view class="mainpadding_top  margin_top6 ffffff radius">
				<view class="flexbetween" @click="pay_type=10">
					<view class="flexleft">
						<u-icon name="weixin-circle-fill" color="#26db6f" size="28"></u-icon>
						<view class="titletext hei_text margin_left">微信</view>
					</view>
					<u-icon name="checkmark-circle-fill" v-if="pay_type==10" color="#ff8505" size="24"></u-icon>
					<u-icon name="checkmark-circle-fill" v-if="pay_type!=10" color="#999" size="24"></u-icon>
				</view>
				<view class="margin_top5 flexbetween" @click="pay_type=30">
					<view class="flexleft">
						<u-icon name="red-packet-fill" color="#ff8505" size="28"></u-icon>
						<view class="titletext hei_text margin_left">钱包</view>
					</view>
					<u-icon name="checkmark-circle-fill" v-if="pay_type==30" color="#ff8505" size="24"></u-icon>
					<u-icon name="checkmark-circle-fill" v-if="pay_type!=30" color="#999" size="24"></u-icon>
				</view>
			</view>
		</view>
		<view class="mainpadding gudingdb">
			<view class="bigbtn" @click="jifenPay()">立即支付</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				pay_price: "",
				pay_type: 10,
				userInfo:{},
				is_login:false,
				shop_id:""
			}
		},
		onLoad(options) {
			// #ifdef MP-WEIXIN
			let _this = this
			const url = decodeURIComponent(options.q) // 获取到二维码原始链接内容
			if (url.indexOf("?") != -1) {
				var str = url.substr(url.indexOf("?") + 1);
				var strs = str.split("&");
				let theRequest = {};
				for (var i = 0; i < strs.length; i++) {
					theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
				}
				_this.shop_id = options.shop_id ? options.shop_id : (theRequest.shop_id ? theRequest.shop_id :
					0);
			}
			// #endif
		},
		onShow() {
			this.is_login = httpRequest.checkIsLogin();
			if (this.is_login) {
				this.init()
			}else{
				httpRequest.toast("未登录，系统自动跳转登录页面")
				setTimeout(()=>{
					uni.navigateTo({
						url:"/pages/login/login"
					})
				},1000)
			}
		},
		methods: {
			init(){
				httpRequest.request('/api/user/index', 'GET', {}).then(res => {
					this.userInfo = res.data
				})
			},
			jifenPay() {
				if (this.pay_type == 30) {
					if (Number(this.userInfo.money) < Number(this.pay_price)) {
						httpRequest.toast("可用余额不足，无法购买")
						return false
					}
				}
				if(this.pay_price==""){
					httpRequest.toast("请输入支付金额")
					return false
				}
				httpRequest.request('/api/Payordercl/rechargeMoney', 'POST', {
					pay_type: this.pay_type,
					price: this.pay_price,
					shop_id:this.shop_id
				}).then(res => {
					if (res.code == 1) {
						if (this.pay_type == 10) { //微信
							let data = res.data.wxconfig
							uni.requestPayment({
								provider: 'wxpay',
								timeStamp: data.timeStamp,
								nonceStr: data.nonceStr,
								package: data.package,
								signType: data.signType,
								paySign: data.paySign,
								success: function(res) {
									// 支付成功
									uni.reLaunch({
										url:"/pages/homepage"
									})
								},
								fail: function(err) {
									// 支付失败
									_this.paycatchresult = true
								}
							});
						} else { //钱包
							httpRequest.toast("支付成功")
							setTimeout(() => {
								uni.reLaunch({
									url:"/pages/homepage"
								})
							}, 1000)
						}
					} else {
						httpRequest.toast(res.msg)
					}
				})
			},
		}
	}
</script>

<style>

</style>
